package com.learn.excel.core.domain;

import com.learn.excel.core.consts.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: 杜海强
 * @description: Http响应实体
 * @date: 2021/4/16
 */
@Data
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("返回对象")
    private T data;
    @ApiModelProperty("返回码")
    private String code;
    @ApiModelProperty("对返回码的文本描述内容")
    private String msg;

    public Response(T data) {
        this.code = ErrorCode.NORMAL.getErrCode();
        this.msg = ErrorCode.NORMAL.getErrMsg();
        this.data = data;
    }

    public Response(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(T data, String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = (data == null ? (T) new Object() : data);
    }

    public static Response success() {
        return new Response(null);
    }

    public static Response success(String message) {
        return new Response(ErrorCode.NORMAL.getErrCode(), message);
    }

    public static <T> Response<T> success(T data) {
        return new Response(data);
    }

    public static <T> Response<T> success(T data, String message) {
        return new Response(data, ErrorCode.NORMAL.getErrCode(), message);
    }

    /***
     * 参数异常
     * @param message
     * @return
     */
    public static Response paramFail(String message) {
        return new Response(ErrorCode.ERR_PARAM_SYSTEM.getErrCode(), message);
    }

    public static Response fail() {
        return new Response(ErrorCode.ERR_CODE_SYSTEM.getErrCode(), ErrorCode.ERR_CODE_SYSTEM.getErrMsg());
    }

    public static Response fail(String message) {
        return new Response(ErrorCode.ERR_CODE_SYSTEM.getErrCode(), message);
    }

    public static Response fail(String code, String message) {
        return new Response(code, message);
    }

    public static Response fail(ErrorCode errorCode) {
        return new Response(errorCode.getErrCode(), errorCode.getErrMsg());
    }

    public static Response fail(Exception e) {
        return new Response(ErrorCode.ERR_CODE_SYSTEM.getErrCode(), e.getMessage());
    }

    public static <T> Response<T> fail(T data, String code, String message) {
        return new Response(data, code, message);
    }
}
